package com.ffz.springbootvuedemo.service.impl;

import com.ffz.springbootvuedemo.bean.User;
import com.ffz.springbootvuedemo.bean.vo.UserEditVO;
import com.ffz.springbootvuedemo.bean.vo.UserVO;
import com.ffz.springbootvuedemo.dao.UserDao;
import com.ffz.springbootvuedemo.service.UserService;
import com.ffz.springbootvuedemo.util.UUIDTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: fengfazheng
 * @create: 2021-12-28 23:43
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(UserVO userVO) {
        User user = new User();
        BeanUtils.copyProperties(userVO, user);
        user.setId(UUIDTool.getUUID());
        userDao.save(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(UserEditVO userEditVO) {
        User user = new User();
        BeanUtils.copyProperties(userEditVO, user);
        userDao.save(user);
    }

    @Override
    public Page<User> getAll(Integer pageNum, Integer pageSize, String key) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        User user = new User();
        user.setNickname(key);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("nickname", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<User> example = Example.of(user, matcher);
        return userDao.findAll(example, pageable);
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }
}
